﻿
@{
    Layout = null;
}



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>产品零件材料工艺管理</title>
    <link href="~/Content/style.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="~/Content/alertify.min.css" />
    <link rel="stylesheet" href="~/Content/themes/default.min.css" />
    <style>
        .pd_left{
            width:280px;
            float:left;
        }
        h3{
            margin:0;
        }
        #box_add2 { width:420px; padding-bottom:25px; display:none; border:1px solid #ccc; position:fixed; top:50%; left:50%; margin-top:-200px;margin-left:-220px ;background:#fff; box-shadow:0 0 10px #ddd; z-index:99;}
        #box_add2 h2 { position:relative;border-bottom: 1px dotted #e9e9e9;background: #fdfdfd;padding-left:20px; line-height:40px; font-weight:normal; font-size:16px; margin-bottom:25px;}
        #box_add2 h2 span { position:absolute;  height:24px; display:block; line-height:25px; top:8px; right:10px; font-size:24px; cursor:pointer;}
        #box_add2 h2 span:hover { color:#ff0000;} 
        #box_add2 div { height:30px; margin-top:10px;}
        #box_add2 div  select{ width: 250px; float: left; height: 28px;}
        #box_add2 span{ line-height:30px; float:left; display:block; width:115px; text-align:right; padding-right:10px;}
        #box_add2 div input{ width:250px; float:left; line-height:28px; height:28px; border:1px solid #ccc; text-indent:10px;    -moz-box-shadow: 2px 2px 5px #e6e6e6 inset;
            -webkit-box-shadow: 2px 2px 5px #e6e6e6 inset;
            box-shadow: 2px 2px 5px #e6e6e6 inset;
            background: #fafafa;}
        #box_add2 div a{ display:inline-block; width:40px; line-height:28px; border:1px solid #ccc;    margin-left: 8px; text-align:center; cursor:pointer;    text-decoration: none;}	
        #box_add2 p{ margin-top:20px; text-align:center;}
        #box_add2 p input{ display:inline-block;padding: 0 20px;border: 1px solid #ddd; margin:0 10px; cursor:pointer; line-height: 30px; height:30px; font-size: 16px;border-radius: 5px;}
    </style>
</head>

<body style="background:#f5f5f5; height:100%">
    @Html.Action("Top","Home")

    <div class="clear"></div>

    <div id="product">
        <div class="pd_left">
            <div class="pro_l">
                <ul style="background:none; display:block;">
                    <li>
                        <p><a href="javascript:;" target="main" data-level="0" data-id="0">我的目录</a></p><em></em><i></i>
                        <ul style="display:block;">
                            
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
        <div class="pro_c" style="width:140px; float:left;">
            <a class="add" onclick="onAddSameItem();">添加同级项目</a>
            <a class="add" onclick="onAddChildItem();">添加下级项目</a>
            <a onclick="onDeleteItem();">删除选择项目</a>
            <a class="add3" onclick="onCopyProduct();">复制整个产品</a>
            <a class="add3" onclick="onFinishProduct();">已录完</a>
        </div>
        <div>
            <iframe name="main" src="/Product/list" height="684" scrolling="No" frameborder="0"></iframe>
        </div>
    </div>

    <!--添加产品-->
    <div id="box_add" class="div_productadd">
        <h2>添加产品<span>×</span></h2>
        <div><span>产品编号：</span><input type="text" name="Sno" required data-type="text" maxlength="50" data-error="产品编号不能为空且长度不能超过50个字符！" /></div>
        <div><span>产品名称：</span><input type="text" name="Name" required data-type="text" maxlength="50" data-error="产品名称不能为空且长度不能超过50个字符！"></div>
        <div><span>客户姓名：</span><input type="text" name="Customer" data-type="text" maxlength="20" data-error="客户姓名长度不能超过20个字符！"></div>
        <div><span>订单编号：</span><input type="text" name="OrderNo" data-type="text" maxlength="50" data-error="订单编号长度不能超过50个字符！"></div>
        <p><input type="submit" class="jb" value="保存" onclick="onAddProduct();"/><input class="jb" type="button" value="返回"></p>
    </div>
    <!--添加零件-->
    <div id="box_add2" class="div_partadd">
        <h2>添加零件信息<span>×</span></h2>
        <input type="hidden" name="part_id" value="" />
        <div><span>零件编号：</span><input name="sno" data-type="text" required maxlength="50" data-error="零件编号不能为空且长度不能超过50个字符！" style="width:200px;" type="text" /><a class="jb" href="javascript:;" onclick="onSelectPartItm();">>></a></div>
        <div><span>零件名称：</span><input name="name" data-type="text" required maxlength="50" data-error="零件名称不能为空且长度不能超过50个字符！" type="text"></div>
        <p><input type="submit" class="jb" value="保存" onclick="onAddPart();" /><input class="jb" type="button" value="返回"></p>
    </div>

    <!--产品信息拷贝-->
    <div id="box_add3">
        <h2>产品信息拷贝<span>×</span></h2>

        <h3><span>目标产品信息</span></h3>
        <div><span>产品编号：</span><input type="text" name="Sno" required maxlength="50" data-type="text" data-error="产品编号不能为空且长度不能超过50个字符！" /></div>
        <div><span>产品名称：</span><input type="text" name="Name" required maxlength="50" data-type="text" data-error="产品名称不能为空且长度不能超过50个字符！" ></div>
        <div><span>客户名称：</span><input type="text" name="Customer" maxlength="20"  data-type="text" data-error="客户姓名长度不能超过20个字符！"  /></div>
        <div><span>订单编号：</span><input type="text" name="OrderNo" maxlength="50"  data-type="text" data-error="订单编号长度不能超过50个字符！" ></div>
        <p><input type="submit" class="jb" value="保存" onclick="onSaveCopy();" /><input class="jb" type="button" value="返回"></p>
    </div>

    <div id="box_sel">
        <h2>选择零件<span>×</span></h2>
        <iframe src="/Product/SearchPart" width="100%" height="100%" frameborder="0"></iframe>
    </div>

    <script type="text/javascript" src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script>
        $("iframe[name='main']").width($(window).width() - 460);
        //$("iframe[name='main']").height($(window).height());
    </script>
    <script src="~/Scripts/menu.js"></script>
    <script src="~/Scripts/uilts.js"></script>
    <script src="~/Scripts/alertify.min.js"></script>
    <script>
        $(function () {
            InitTree();
            $('#box_add,#box_add2,#box_add3').find('p input').eq(1).click(function () {
                $(this).parent().parent().css("display", "none");
            });
            $('#box_add,#box_add2,#box_add3,#box_sel').find('h2 span').click(function () {
                $(this).parent().parent().css("display", "none");
            });
        });

        function InitTreeEvent() {
            $('.pro_l p').click(function () {
                var evt_obj = this;
                $('.pro_l p').removeClass('active');
                $(evt_obj).addClass('active');

                var selected = GetSelectItem();
                if (selected.level == 0) {
                    $("iframe[name='main']").attr("src", "/Product/list");
                } else if (selected.level == 1) {
                    $("iframe[name='main']").attr("src", "/Product/Partlist?parent_id="+selected.id+"&partid=" + selected.partid + '&productid=' + selected.productid);
                } else if (selected.level == 2) {
                    if ($(evt_obj).next()[0].nodeName.toLowerCase() == "i") {
                        $("iframe[name='main']").attr("src", "/Product/Partlist?parent_id=" + selected.id + "&partid=" + selected.partid + '&productid=' + selected.productid);
                    } else {
                        $("iframe[name='main']").attr("src", "/Product/Materiallist?partid=" + selected.partid + '&productid=' + selected.productid + "&pp_id=" + selected.id);
                    }
                }
            });
            $('.pro_l i').attr('onoff', 1);
            $('.pro_l i').click(function () {
                if ($(this).attr('onoff') == 1) {
                    $(this).addClass('on');
                    $(this).next().css("display", "block");
                    $(this).attr('onoff', 0);
                } else {
                    $(this).removeClass('on');
                    $(this).next().css("display", "none");
                    $(this).attr('onoff', 1);
                }
            });
        }
    </script>
    <script>

        function InitChildItem(data, product_id) {
            var html = "";
            if (data != undefined && data.length > 0) {
                $.each(data, function (index, value) {
                    var html_product = "";
                    var html_part = "";
                    if (value.childs != undefined && value!=null && value.childs.length > 0) {
                        html_product = '<p><a href="javascript:;" data-level ="2" data-part="' + value.id + '" data-product="' + product_id + '" data-id="' + value.pp_id + '" target="' + value.sno + '">' + value.name + '</a></p><i></i>';
                        html_part = InitChildItem(value.childs, product_id);
                    } else {
                        html_product = '<p><a href="javascript:;" data-level ="2" data-part="' + value.id + '" data-product="' + product_id + '" data-id="' + value.pp_id + '" target="' + value.sno + '">' + value.name + '</a></p><span></span>';
                    }
                    html_part = '<ul>' + html_part + '</ul>';
                    html += '<li>' + html_product + html_part + '</li>';
                });
            }
            return html;
        }

        function InitTree() {
            PostApi('/Product/GetTree', { product_id : 0 }, function (result) {
                if (result.code == 0) {
                    $("div#product>div.pd_left>div.pro_l>ul>li:first>ul").empty();
                    var html = '';
                    $.each(result.data, function (index, value) {
                        var html_product = '<p><a href="javascript:;" data-level ="1" data-part=0 data-product="' + value.product_id + '" target="' + value.sno + '">' + value.name + '<b>' + (value.IsFinished != 1 ? '（未录完）' : '') + '  </b></a></p><i></i>';
                        var html_part = "";
                        if (value.Parts != undefined && value.Parts.length > 0) {
                            html_part = InitChildItem(value.Parts, value.product_id);
                        }
                        html_part = '<ul>' + html_part + '</ul>';
                        html += '<li>' + html_product + html_part + '</li>';
                    });
                    $("div#product>div.pd_left>div.pro_l>ul>li:first>ul").append(html);
                    InitTreeEvent();
                } else {
                    alertify.error(result.ErrorMessage == undefined ? "产品树初始化失败！" : result.ErrorMessage);
                }
            }, function () {

            });
        }

        function onAddSameItem() {
            var st = GetSelectItem();
            if (st.level == 1) {
                //添加产品
                $("div.div_productadd").css("display", "block");
            } else if (st.level == 2) {
                //添加零件
                $("div.div_partadd").css("display", "block");
            }
        }

        function onAddChildItem() {
            var st = GetSelectItem();
            if (st.level == 0) {
                //添加产品
                $("div.div_productadd").css("display","block");
            } else if (st.level == 1 || st.level == 2) {
                //添加零件
                $("div.div_partadd").css("display", "block");
            }
        }

        function onDeleteItem() {
            var data = GetSelectItem();
            if (data.level <= 0) {
                alertify.error("未选中");
                return;
            }
            alertify.confirm("提示", "确定删除?", function () {
                PostApi("/Product/DeleteItem", { product_id: data.productid, pp_id: data.id }, function (result) {
                    if (result.code == 0) {
                        InitTree();
                        alertify.success("操作完成！");
                    } else {
                        alertify.error(result.ErrorMessage);
                    }
                }, function () {
                    alertify.error("删除失败！");
                })
            }, function () { });
        }

        function onFinishProduct() {
            var data = GetSelectItem();
            if (data.level != 1) {
                alertify.error("未选中产品！");
                return;
            }
            PostApi("/Product/IsFinished", { product_id: data.productid }, function (result) {
                if (result.code == 0) {
                    InitTree();
                    alertify.success("完成产品成功！");
                } else {
                    alertify.error(result.ErrorMessage);
                }
            }, function () {
                alertify.error("完成产品失败！");
            });
        }

        var copy_id = 0;
        function onCopyProduct() {
            var st = GetSelectItem();
            if (st.level == 1) {
                copy_id = st.productid;
                $("#box_add3").css("display", "block");
            }
        }

        function onSaveCopy() {
            if (!ValidateModel("#box_add3", function (error) {
              alertify.error(error);
            })) {
                return;
            };
            var data = GetFormData("#box_add3");
            data["product_id"] = copy_id;

            PostApi("/Product/CopyProduct", data, function (result) {
                if (result.code == 0) {
                    alertify.success("复制成功!");
                    EmptyForm("#box_add3");
                    InitTree();
                } else {
                    alertify.error(result.ErrorMessage);
                }
            }, function () {
                alertify.error("产品复制失败!");
            });
        }

        function GetSelectItem(){
            var selectP = $("#product p.active>a");
            if (selectP == undefined || selectP.length == 0);
                //alert("未选中");

            var level = selectP.attr("data-level") || -1;
            var productid = selectP.attr("data-product") || -1;
            var partid = selectP.attr("data-part") || -1;
            var id = selectP.attr("data-id") || 0;

            return {
                productid: productid,
                partid: partid,
                level: level,
                id:id
            }
        }

        function onAddProduct() {
            if (!ValidateModel("div.div_productadd", function (error) {
                alertify.error(error);
            })) {
                return;
            };
            var data = GetFormData("div.div_productadd");
            PostApi("/Product/AddProduct", JSON.stringify(data), function (result) {
                if (result.code == 0) {
                    alertify.success("添加成功!");
                    EmptyForm("div.div_productadd");
                    InitTree();
                    $('iframe[name="main"]').attr('src', $('iframe[name="main"]').attr('src'));
                } else {
                    alertify.error(result.ErrorMessage);
                }
            }, function () {
                alertify.error("添加失败!");
            }, "application/json");
        }

        function onAddPart() {
            if (!ValidateModel("#box_add2", function (error) {
                alertify.error(error);
            })) {
                return;
            };
            var data = GetFormData("#box_add2");
            var sitem = GetSelectItem();
            data["product_id"] = sitem.productid;

            if (data["name"] != select_name || data["sno"] != select_sno)
                data["part_id"] = 0;
            data["parent_part_id"] = sitem.id;

            PostApi("/Product/AddPart", data, function (result) {
                if (result.code == 0) {
                    EmptyForm("#box_add2");
                    alertify.success("零件添加成功！");
                    InitTree();
                    $('iframe[name="main"]').attr('src', $('iframe[name="main"]').attr('src'));
                }else{
                    alertify.error(result.ErrorMessage);
                }
            }, function () {
                alertify.error("添加零件失败！");
            });
        }

        function onSelectPartItm() {
            $("#box_sel").css("display","block");
        }
        
        var select_sno = "";
        var select_name = "";
        function partSelectCallback(data) {
            $("#box_sel").css("display", "none");
            select_sno = data.sno;
            select_name = data.name;
            $("#box_add2 input[name='sno']").val(data.sno);
            $("#box_add2 input[name='name']").val(data.name);
            $("#box_add2 input[name='part_id']").val(data.id);
        }

        function CloseSelectPartDiv() {
            $("#box_sel").css("display", "none");
        }

    </script>

</body>
</html>